package org.groupg.project;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GuavaCollectionsDemo {

    public static void main(String[] args) {
        // 1. 不可变集合
        ImmutableList<String> colors = ImmutableList.of("Red", "Green", "Blue");
        ImmutableSet<Integer> primes = ImmutableSet.of(2, 3, 5, 7, 11);
        ImmutableMap<String, Integer> wordScores = ImmutableMap.<String, Integer>builder()
            .put("apple", 5)
            .put("banana", 7)
            .put("cherry", 6)
            .build();

        System.out.println("不可变集合:");
        System.out.println("颜色: " + colors);
        System.out.println("质数: " + primes);
        System.out.println("单词分数: " + wordScores);

        // 2. 多值集合 (Multimap)
        Multimap<String, String> library = ArrayListMultimap.create();
        library.put("Fiction", "Harry Potter");
        library.put("Fiction", "The Hobbit");
        library.put("Science", "A Brief History of Time");
        library.putAll("Programming", Arrays.asList("Clean Code", "Design Patterns"));

        System.out.println("\n图书馆分类:");
        for (Map.Entry<String, String> entry : library.entries()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // 3. BiMap（双向映射）
        BiMap<String, Integer> countryCodes = HashBiMap.create();
        countryCodes.put("United States", 1);
        countryCodes.put("United Kingdom", 44);
        countryCodes.put("Japan", 81);

        System.out.println("\n国家代码:");
        System.out.println("美国代码: " + countryCodes.get("United States"));
        System.out.println("代码44的国家: " + countryCodes.inverse().get(44));

        // 4. Table（二维表格）
        Table<String, String, Double> distanceTable = HashBasedTable.create();
        distanceTable.put("London", "Paris", 344.0);
        distanceTable.put("New York", "Los Angeles", 3940.0);
        distanceTable.put("Tokyo", "Seoul", 1150.0);

        System.out.println("\n城市间距离:");
        System.out.println("伦敦到巴黎: " + distanceTable.get("London", "Paris") + " km");

        // 5. 函数式操作
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva");
        List<String> filteredNames = Lists.newArrayList(
            Collections2.filter(names, name -> name.length() > 4));

        System.out.println("\n过滤后的名字: " + filteredNames);

        // 6. 缓存实现
        LoadingCache<String, String> cache = CacheBuilder.newBuilder()
            .maximumSize(3)
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .recordStats()
            .build(new CacheLoader<String, String>() {
                @Override
                public String load(String key) {
                    return expensiveDatabaseCall(key);
                }
            });

        cache.put("user1", "John Doe");
        cache.put("user2", "Jane Smith");
        cache.put("user3", "Bob Johnson");

        System.out.println("\n缓存内容:");
        System.out.println("user1: " + cache.getIfPresent("user1"));
        cache.invalidate("user1");
        System.out.println("user1失效后: " + cache.getIfPresent("user1"));

        // 7. 集合工具
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<List<Integer>> partitions = Lists.partition(numbers, 2);
        System.out.println("\n分区结果: " + partitions);
    }

    private static String expensiveDatabaseCall(String key) {
        return "Data for " + key;
    }
}